<?php
namespace app\admin\controller;
/**
 * #菜单管理#
 */
class Navs extends Common{
	/**
	 * #菜单列表#
	 * @param  string $type 模块 admin || member
	 * @return view
	 */
	function index($type='admin'){
		$list=model($this->table)->getNavs($type);
		$this->assign('list',json_encode($list));
		$this->assign('type',$type);
		return view();
	}
	/**
	 * #菜单编辑、新增#
	 * @param integer $id 编辑时存在菜单ID
	 */
	function add($id=0){
        $nav=[];
        if($id>0){
            $nav=model($this->table)->read(['id'=>$id]);
            //该项不存在或不属于该用户 做添加操作
            if(empty($nav)){
                $id=0;
            }
        }
        if(request()->isPost()){
            $data=[
                'id'=>$id,
                'pid'=>input('pid/d',0),
                'name'=>input('name/s',''),
                'title'=>input('title/s',''),
                'status'=>input('status/d',1),
                'type'=>input('type/s','admin'),
                'icon'=>input('icon/s',''),
                'params'=>input('params/s'),
            ];
            $validate=$this->validate($data,[
            	'title|菜单名'=>'require',
            	'name|Name'=>'require',
            ]);
            if(true!==$validate){
            	$this->error($validate);
            }
            if($data['pid']>0){
            	$has=model($this->table)->where(['id'=>$data['pid']])->count();
            	if(!$has){
            	    $this->error('父级不存在');
            	}
            }
            $operation=[];
            if($id>0){
                $operation=model($this->table)->edit($data);
            }else{
            	$data['sort']=time();
                $operation=model($this->table)->add($data);
            }
            if(empty($operation)){
                $this->error(lang('Failed operation'));
            }
            //更新菜单缓存
            $this->updateCache($data['type']);
            $this->success(lang('Successful operation'));
        }
	}
	// 菜单删除
	function del($id=0){
		if(request()->isAjax()||request()->isPost()){
			if($id<=0){
				$this->error(lang('Data does not exist'));
			}
			$res=model($this->table)->delById($id);
			if($res['code']){
			    $this->error($res['msg']);
			}
			//更新菜单缓存
			$this->updateCache();
			$this->success($res['msg']);
		}

	}
	/**
	 * 隐藏、显示菜单
	 * @param  integer $id   ID
	 * @param  string  $type 类型
	 * @return json
	 */
	function close($id=0,$type='admin'){
		if(request()->isAjax()||request()->isPost()){
			if($id<=0){
				$this->error(lang('Data does not exist'));
			}
			$rs=model($this->table)->close("id={$id}",'status',true);
			if($rs){
				$this->updateCache($type);
				$this->success(lang('Successful operation'));
				die;
			}
			$this->error(lang('Failed operation'));
		}
	}
	/**
     *菜单排序上下移动
     * @param  array  	$val [{id,sort}]
     * @param  string  $type 类型
     * @return json
     */
    function record($val=[],$type='admin'){
    	if(request()->isAjax()||request()->isPost()){
	    	if(empty($val)){
	    		$this->error(lang('Data does not exist'));
	    	}
	    	$ids=[];
	    	foreach($val as $v) {
	    		if(!isset($v['id'])||$v['id']<=0||!isset($v['sort'])||$v['sort']<0){
	    			$this->error('数据有误');
	    		}
	    		array_push($ids,$v['id']);
	    	}
	    	$where=[
	    		'id'=>['id','in',$ids],
	    	];
	    	$nums=db($this->table)->where($where)->count();
	    	if($nums!=count($ids)){
	    		$this->error('数据有误');
	    	}
	    	$save=model($this->table)->edit($val);
	    	if(!$save){
	    		$this->error(lang('Failed operation'));
	    	}
	    	$this->updateCache($type);
	    	$this->success(lang('Successful operation'));
    	}
    }
	/**
	 * 更新菜单缓存
	 * @param  @param  string  $type  类型 admin | member | null
	 * @return void
	 */
	function updateCache($type=null){
		if(is_null($type)){
			//后台菜单
			model($this->table)->exportNavs('admin');
			//会员中心菜单
			model($this->table)->exportNavs('member',true);
		}else{
			//后台菜单
			model($this->table)->exportNavs($type,true);
		}
		if(request()->isAjax()){
            $this->success(lang('Successful operation'));
		}
	}
}